package com.kgc.controller;

import com.kgc.document.Ticket;
import com.kgc.pojo.DeliveryInfo;
import com.kgc.pojo.Order;
import com.kgc.pojo.TicketInfo;
import com.kgc.pojo.TicketType;
import com.kgc.service.DeliveryinfoService;
import com.kgc.service.GetOrderInfoService;
import com.kgc.service.GetTicketDetailInfo_wzlService;
import com.kgc.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 支付页面控制器
 * 王臻利
 */
@RestController
public class PaymentController {

    @Autowired
    private GetTicketDetailInfo_wzlService getTicketDetailInfo_wzlService;
    @Autowired
    private GetOrderInfoService getOrderInfoService;
    @Autowired
    private DeliveryinfoService deliveryinfoService;
    @PostMapping("checkTicketInfo")
    public Object checkTicketInfo(int Tickeinfoid, int TicketTypeid, int TicketType_count){
        Map<String,Object> map  = new HashMap<String,Object>();
        /*查询标题*/
        TicketInfo ticketInfo =getTicketDetailInfo_wzlService.checkTicketInfo(Tickeinfoid);
        /*查询类型坐票*/
        TicketType ticketType =getTicketDetailInfo_wzlService.selectTicketTypeByPrimaryKey(TicketTypeid);
        /*更新数据库票数*/
        int ticketCount = getTicketDetailInfo_wzlService.updateTicketCounts(TicketTypeid,TicketType_count);
        map.put("title",ticketInfo.getTitle());//获取标题
        map.put("time",ticketInfo.getTime());//获取时间
        map.put("picpath",ticketInfo.getPicpath());//获取图片
        map.put("hostAddress",ticketInfo.getHostaddress());//获取场馆
        map.put("type",ticketType.getType());//获取票类
        map.put("price",ticketType.getPrice());//获取单价
        return  new Result(map,"信息查询成功");
    }
    /*插入订单信息*/
    @PostMapping("InserOrderinfo")
    public Object InserOrderinfo(Order order) throws ParseException {

        int count = getOrderInfoService.inserOrederinfo(order);

        return new Result(count,"good");
    }
    /*插入时间*/
    @PostMapping("InserOrdertime")
    public Object InserOrderinfo(String time) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        java.util.Date createdate = df.parse(time);
        //获取order当前最新的ID
        Order order1 =getOrderInfoService.selectorderLastInfo();
        //更新创建的时间
        int   count2 = getOrderInfoService.updatecreatedate(createdate,order1.getId());
        return  new Result(count2,"插入时间成功");
    }
    /*插入deliveryinfo*/
    @PostMapping("inserDeliveryinfo")
    public Object inserDeliveryinfo(DeliveryInfo deliveryInfo){
        int count = deliveryinfoService.inserDeliveryInfo(deliveryInfo);
        return new Result(count, "deliveryinfo信息插入成功");
    }
    /*插入价格数值*/
    @PostMapping("inserDeliveryinfoprice")
    public Object inserDeliveryinfoprice(String totalprice,long carriage){
        double num =Double.parseDouble(totalprice);
        DeliveryInfo deliveryInfo= deliveryinfoService.selectdeliveryLastInfo();
        int  count =deliveryinfoService.inserDeliveryPrice(carriage,num,deliveryInfo.getId());
        return new Result(count,"插入成功");
    }

}
